package cn.tb.org.controller;

import cn.tb.basic.anno.PreAuthorize;
import cn.tb.basic.util.AjaxResult;
import cn.tb.basic.util.PageList;
import cn.tb.org.domain.Employee;
import cn.tb.org.query.EmployeeQuery;
import cn.tb.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    /**
     * 查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.findById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    //@PreAuthorize(name = "获取所有员工",value = "emp:findAll")
    public List<Employee> queryAll(){
        return employeeService.findAll();
    }

    /**
     * 新增或者修改
     */
    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Employee employee){
        try {
            if(employee.getId() == null){
                //新增
                employeeService.save(employee);
            }else{
                //修改
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("抱歉哦，系统错误，我们正在处理");
        }
    }

    /**
     * 高级查询+分页
     */
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult romave(@PathVariable Long id){
        try {
            employeeService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("抱歉哦，系统错误，我们正在处理");
        }
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody Long[] ids){
        try {
            employeeService.patchRemove(ids);
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("抱歉哦，系统错误，我们正在处理");
        }
    }

}
